﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OCRTheNewHope
{
    public enum RectangleType
    { 
        Normal = 0,
        Rotated = 1,
    }

    public class Rectangle
    {
        public static Point LeftTop = new Point(4, 4);
        public static Point RightBottom = new Point(24, 24);

        public static Random rnd = new Random();

        public int Width { get; private set; }
        public int Height { get; private set; }

        public RectangleType rectangleType { get; private set; }

        public Point A { get; private set; }
        public Point B { get; private set; }
        public Point C { get; private set; }
        public Point D { get; private set; }

        // A  B
        // C  D

        public Rectangle(RectangleType rectangleType)
        {
            if (rectangleType == RectangleType.Normal)
            {
                int imageWidth = RightBottom.X - LeftTop.X;
                int imageHeight = RightBottom.Y - LeftTop.Y;

                while (this.Width == 0 || this.Height == 0)
                {
                    int x1, y1, x2, y2;

                    do
                    {
                        x1 = rnd.Next(imageWidth) + LeftTop.X;
                        y1 = rnd.Next(imageHeight) + LeftTop.Y;

                        x2 = rnd.Next(imageWidth) + LeftTop.X;
                        y2 = rnd.Next(imageHeight) + LeftTop.Y;
                    }
                    while (x1 == x2 && y1 == y2);

                    int xMin = Math.Min(x1, x2);
                    int yMin = Math.Min(y1, y2);

                    int xMax = Math.Max(x1, x2);
                    int yMax = Math.Max(y1, y2);

                    this.A = new Point(xMin, yMin);
                    this.D = new Point(xMax, yMax);

                    this.Width = this.D.X - this.A.X;
                    this.Height = this.D.Y - this.A.Y;

                    this.B = new Point(this.A.X + this.Width, this.A.Y);
                    this.C = new Point(this.A.X, this.A.Y + this.Height);
                }
            }
            else if (rectangleType == RectangleType.Rotated)
            {
                int imageWidth = RightBottom.X - LeftTop.X;
                int imageHeight = RightBottom.Y - LeftTop.Y;

                while (this.Width == 0 || this.Height == 0)
                {
                    int x, y, width, height;
                    int side;

                    side = rnd.Next(1);  // should be 4

                    width = rnd.Next(imageWidth);
                    height = rnd.Next(imageHeight);


                    switch (side)
                    {
                        case 0:

                            while (true)
                            {
                                x = rnd.Next(imageWidth) + LeftTop.X;
                                y = rnd.Next(imageHeight) + LeftTop.Y;

                                double heightShift = height / Math.Sqrt(2);
                                double widthShift = width / Math.Sqrt(2);

                                int heightShiftInteger = (int)Math.Round(heightShift, 0, MidpointRounding.AwayFromZero);

                                int widthShiftInteger = (int)Math.Round(widthShift, 0, MidpointRounding.AwayFromZero);

                                this.D = new Point(x, y);

                                this.B = new Point(x + heightShiftInteger, y + heightShiftInteger);

                                this.C = new Point(x - widthShiftInteger, y + widthShiftInteger);

                                this.A = new Point(x + heightShiftInteger - widthShiftInteger, y + heightShiftInteger + widthShiftInteger);

                                if (CheckValidity(new Point[] { this.A, this.B, this.C, this.D }))
                                {
                                    break;
                                }
                            }
                            break;
                        case 1:
                            break;
                        case 2:
                            break;
                        case 3:
                            break;
                    }


                }                                
            }
        }

        public Rectangle(Point solUst, Point sagAlt)
        {
            // TODO: Complete member initialization
            this.A = solUst;
            this.D = sagAlt;

            this.Width = this.D.X - this.A.X;
            this.Height = this.D.Y - this.A.Y;

            this.B = new Point(this.A.X + this.Width, this.A.Y);
            this.C = new Point(this.A.X, this.A.Y + this.Height);
        }

        public override string ToString()
        {
            return "(" + this.A.X.ToString() + ", " + this.A.Y.ToString() + "), W: " + this.Width.ToString() + ", H: " + this.Height.ToString();
        }

        public bool CheckValidity(Point[] points)
        {
            bool isValid = true;
            foreach (Point p in points)
                if (p.X < 0 || p.X > 27 || p.Y < 0 || p.Y > 27)
                    isValid = false;

            return isValid;
        }
    }
}
